'use strict';

/**
 * @name TechListCtrl
 * @author ZYF
 */

angular.module('metalabOperationApp').controller('TechListCtrl', ['$scope', '$rootScope', '$state', '$stateParams', 'MetaResources', 'MetaRemind', 'MetaFilter', 'MetaSearch', 'MetaSort', 'MetaUtils', function($scope, $rootScope, $state, $stateParams, MetaResources, MetaRemind, MetaFilter, MetaSearch, MetaSort, MetaUtils) {
    $scope.listCtrl = {
        filterConfig: ['area_choice', 'rank', 'origin', 'manager', 'state', 'created', 'verified', 'technologyForm'],
        filterParams: {},
        searchConfig: {
            sn_like: '编号',
            title_like: '技术名称'
        },
        searchParams: {},
        sortParams: '-created_at',
        tableParams: {}
    };

    $scope.init = function() {
        MetaUtils.initListCtrl($scope, $stateParams);
    };
    $scope.getListData = function(page) {
        var queryParams = {
            '_embed': 'publisher,leader,projectManager',
            '_sort': '',
            '_range': '',
            'areas': '',
            'technologyForm': '',
            'origin': '',
            'projectManagerId': '',
            'state': $stateParams.state,
            'version': 'online'
        };
        var dynamicQueryParams = ['rank'];
        MetaUtils.buildQueryParams(page, queryParams, dynamicQueryParams, $scope.listCtrl, $stateParams);
        MetaResources.technology.query(
            queryParams,
            function(data, header) {
                MetaUtils.receiveListData($scope.listCtrl, $stateParams, page, data, header);
                angular.forEach(data, function(value) {
                    value.demandName = '';
                    MetaResources.technology.queryNum({
                            'technology_id': value.id,
                            '_range': '1_1',
                            '_embed': 'demand'
                        },
                        function(data, header) {
                            if ($state.includes('meta.permission.technology.technology')) {
                                value.numberOfApplying = parseInt(header()['x-count']);
                            } else {
                                value.demandName = data[0].demand.title;
                            }
                        },
                        function(error) {
                            MetaRemind.serverError(error);
                        });
                    MetaResources.technology.queryNum({
                            'technology_id': value.id,
                            '_range': 0,
                            '_embed': 'demand',
                            'state_like': '已完成'
                        },
                        function(data, header) {
                            value.numberOfApplied = parseInt(header()['x-count']);
                        },
                        function(error) {
                            MetaRemind.serverError(error);
                        });
                });
            },
            function(error) {
                MetaRemind.serverError(error);
            });
    };
    $scope.changeUrl = function() {
        var urlParams = {
            page: $scope.listCtrl.tableParams.currentPage,
            sort: $scope.listCtrl.sortParams,
            showAll: $scope.listCtrl.tableParams.isShowAll
        };
        MetaFilter.encodeURLFilterParams(urlParams, $scope.listCtrl.filterParams);
        MetaSearch.encodeUrlSearchParams(urlParams, $scope.listCtrl.searchParams);
        $state.go(
            $state.includes('meta.permission.technology.technology') ? 'meta.permission.technology.technology.list' : 'meta.permission.technology.solution.list',
            urlParams);
    };
    $scope.init();
}]);
